<template>
  <div>
    <el-form :rules="rules"
             ref="loginForm"
             :model="loginForm"
             class="loginContainer"
             v-loading="loading"
             element-loading-text="正在登陆..."
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)">
      <h3>系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="false" placement="请输入用户名" ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input id="loginPassword" placeholder="请输入密码" v-model="loginForm.password" show-password></el-input>
<!--        <el-input type="password" v-model="loginForm.password" auto-complete="false" id="loginPassword"></el-input>-->
      </el-form-item>
      <el-form-item prop="code">
        <el-input type="text" v-model="loginForm.code" placeholder="点击图片更换验证码" auto-complete="false" style=" width: 65% ;
    margin-left: 0px;"></el-input>
        <img :src="captchaUrl" @click="updateCaptchaUrl">
      </el-form-item >
      <el-checkbox v-model="loginForm.rememberMe" class="loginRemember">记住我</el-checkbox>
      <br>
      <el-row>
        <el-col :span="24"><div class="grid-content bg-purple-dark"></div></el-col>
      </el-row>
      <el-form-item>
        <el-col :span="2"></el-col>
        <el-col :span="20">
          <el-button type="primary" style="width: 60%" @click="loginSubmit('loginForm')">登录</el-button>
          <el-button  @click="resetBtn('loginForm')">重置</el-button>
        </el-col>
      </el-form-item>

    </el-form>
  </div>

</template>

<script>

import {delCookie, postRequest} from "../../utils/api";

export default {
  name: "login",
  comments:{

  },
  data(){
    return{
      captchaUrl:'/captcha?time=' + new Date(),
      loginForm:{
        username: '',
        password: '',
        code: '',
        rememberMe: true
      },
      loading:false,
      rules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max:4, message: '请输入 4 位数的验证码', trigger: 'blur' }
        ]

      },
      value: [30, 60],
      marks: {
        0: '0°C',
        8: '8°C',
        37: '37°C',
        50: {
          style: {
            color: '#1989FA'
          },
          label: this.$createElement('strong', '50%')
        }
      }
    }
  },
  methods: {
    loginSubmit(formName){
      this.$refs[formName].validate((value)=>{
        if(value){
          postRequest("/login",this.loginForm).then(resp=>{
            // this.loading = true;
            // alert(JSON.stringify(massage))
            if (resp){
              // 存储用户 token
              const tokenStr = resp.result.tokenHead +resp.result.token;
              window.sessionStorage.setItem('tokenStr',tokenStr);
              this.loading = false;

              //跳转页面
              let path = this.$route.query.redirect;
              this.$router.replace((path == '/' || path == undefined)?'/home':path);
            }

          })
        } else {
          this.loading = false;
          this.$message.error("输入所有字段");
          return false;
        }
      });
    },
    resetBtn(formName){
      this.$refs[formName].resetFields();
    },
    updateCaptchaUrl(){
      this.captchaUrl = '/captcha?time=' + new Date();
      delCookie();
    }

  }
}

</script>

<style >
  .loginContainer{
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 15px 35px 15px 35px;
    background: antiquewhite;
    border: 1px solid azure;
    box-shadow: 0 0 25px beige;
  }
  .loginRemember{
    margin: 0px 0px 10px -280px;
  }
  .el-form-item__content{
    display: flex;
    align-content: center;
  }

</style>
